﻿using RuoVea.WeighIns.Server.Language;

namespace RuoVea.WeighIns.Web.Controllers;

/// <summary>
/// 时段字典控制器
/// </summary>
[Authorize(Roles = "None")]
public partial class InstMeridiemController : Controller
{
    /// <summary>
    /// 时段字典服务
    /// </summary>
    private readonly MeridiemService _meridiemService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="meridiemService"></param> 
    public InstMeridiemController(MeridiemService meridiemService)
    {
        _meridiemService = meridiemService;
    }

    /// <summary>
    /// 时段字典管理页面
    /// </summary>
    /// <returns></returns>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("meridiem/pages")]
    public async Task<RestfulResult<PageResult<InsMeridiem>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.upload_data_required);
        InstMeridiemPageParam? data = JsonConvert.DeserializeObject<InstMeridiemPageParam>(dataStr);
        data ??= new InstMeridiemPageParam { PageNo = 1, PageSize = 10 };

        var query = await _meridiemService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 获取所有时段字典列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("meridiem/list")]
    public async Task<RestfulResult<PageResult<InsMeridiem>>> GetlistAsync()
    {
        var restful = await _meridiemService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 根据代码获取时段字典
    /// </summary>
    /// <param name="code">时段代码</param>
    /// <returns></returns>
    [HttpGet("meridiem/getByCode/{code}")]
    public async Task<RestfulResult<InsMeridiem>> GetByCodeAsync(string code)
    {
        var restful = await _meridiemService.GetByCodeAsync(code);
        return restful;
    }

    /// <summary>
    /// 增加时段字典
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("meridiem/add")]
    public async Task<RestfulResult> AddAsync([FromBody] InsMeridiem data)
    {
        var restful = await _meridiemService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新时段字典
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("meridiem/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] InsMeridiem data)
    {
        var restful = await _meridiemService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除时段字典
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("meridiem/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _meridiemService.DeleteAsync(data);
        return restful;
    }

    ///// <summary>
    ///// 批量删除时段字典
    ///// </summary>
    ///// <param name="ids">ID列表</param>
    ///// <returns></returns>
    //[HttpPost("meridiem/batchDelete")]
    //public async Task<RestfulResult> BatchDeleteAsync([FromBody] List<long> ids)
    //{
    //    var restful = new RestfulResult() { Code = CodeStatus.BadRequest };

    //    if (ids == null || ids.Count == 0)
    //    {
    //        restful.Message = $"{i18n.delete}{i18n.fail}";
    //        return restful;
    //    }

    //    try
    //    {
    //        bool result = await _meridiemService.DeleteAsync(u => ids.Contains(u.Id));
    //        if (result)
    //        {
    //            restful.Data = result;
    //            restful.Message = $"{i18n.delete}{i18n.success}";
    //            restful.Code = CodeStatus.OK;
    //            return restful;
    //        }
    //        else
    //        {
    //            restful.Data = result;
    //            restful.Message = $"{i18n.delete}{i18n.fail}";
    //            return restful;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        restful.Message = $"{i18n.delete}{i18n.fail}：{ex.Message}";
    //        return restful;
    //    }
    //}

    ///// <summary>
    ///// 启用/禁用时段字典
    ///// </summary>
    ///// <param name="data"></param>
    ///// <returns></returns>
    //[HttpPost("meridiem/toggleStatus")]
    //public async Task<RestfulResult> ToggleStatusAsync([FromBody] ToggleStatusParam data)
    //{
    //    var restful = new RestfulResult() { Code = CodeStatus.BadRequest };

    //    //if (data.Id == 0)
    //    //{
    //    //    restful.Message = $"{i18n.parameter_error}";
    //    //    return restful;
    //    //}

    //    try
    //    {
    //        var entity = await _meridiemService.GetByIdAsync(data.Id);
    //        if (entity == null)
    //        {
    //            restful.Message = $"{i18n.not_exist}";
    //            return restful;
    //        }

    //        entity.IsDisable = data.IsDisable;
    //        bool result = await _meridiemService.UpdateAsync(entity);

    //        if (result)
    //        {
    //            restful.Data = result;
    //            restful.Message = $"{i18n.modify}{i18n.success}";
    //            restful.Code = CodeStatus.OK;
    //            return restful;
    //        }
    //        else
    //        {
    //            restful.Message = $"{i18n.modify}{i18n.fail}";
    //            return restful;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        restful.Message = $"{i18n.modify}{i18n.fail}：{ex.Message}";
    //        return restful;
    //    }
    //}
}
